import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["page", "size"];

/**
 * @date 2018-09-17
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryMapIdpValue, getPublicKey as _getPublicKey } from 'cpaas-front/lib/services/api';
import { fetchDataSourceList as _fetchDataSourceList, createDataSource as _createDataSource, editDataSource as _editDataSource, addService as _addService, fetchServiceList as _fetchServiceList, deleteService as _deleteService, handleTestDataSource as _handleTestDataSource, getDbPoolParams as _getDbPoolParams, getDriverClass as _getDriverClass, fetchDataSourceDetail as _fetchDataSourceDetail, fetchFormParams as _fetchFormParams, testConnection as _testConnection } from "../services/dataSourceService";
export default {
  namespace: 'dataSource',
  state: {
    dataSourceData: {},
    // 查询数据列表
    detaSourceDetail: {},
    // 数据源详情数据
    datasourceId: undefined,
    // 数据源id
    tenantData: {},
    // 租户列表
    dbPoolParams: {},
    // 连接池参数数据
    dataSourceClassList: [],
    // 数据库分类值集
    dataSourceTypeList: [],
    // 数据库类型值集
    purposeType: [],
    // 数据源用途
    dbPoolTypeList: [],
    // 连接池类型值集
    dsPurposeCodeList: [],
    // 数据源用途值集
    driverTypeCodeList: [],
    // 数据源用途值集
    pagination: {},
    // 分页对象
    extConfigs: [],
    // 表单配置信息
    publicKey: '' // 密码公钥

  },
  effects: {
    // 统一获取值级的数据
    batchCode: /*#__PURE__*/_regeneratorRuntime.mark(function batchCode(_ref, _ref2) {
      var payload, put, call, lovCodes, code, _code$dataSourceClass, dataSourceClassList, _code$dataSourceType, dataSourceTypeList, _code$dbPoolType, dbPoolTypeList, _code$dsPurposeCode, dsPurposeCodeList, _code$driverTypeCode, driverTypeCodeList, _code$purposeType, purposeType;

      return _regeneratorRuntime.wrap(function batchCode$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              lovCodes = payload.lovCodes;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context.t1 = _context.sent;
              code = (0, _context.t0)(_context.t1);

              if (!code) {
                _context.next = 12;
                break;
              }

              _code$dataSourceClass = code.dataSourceClass, dataSourceClassList = _code$dataSourceClass === void 0 ? [] : _code$dataSourceClass, _code$dataSourceType = code.dataSourceType, dataSourceTypeList = _code$dataSourceType === void 0 ? [] : _code$dataSourceType, _code$dbPoolType = code.dbPoolType, dbPoolTypeList = _code$dbPoolType === void 0 ? [] : _code$dbPoolType, _code$dsPurposeCode = code.dsPurposeCode, dsPurposeCodeList = _code$dsPurposeCode === void 0 ? [] : _code$dsPurposeCode, _code$driverTypeCode = code.driverTypeCode, driverTypeCodeList = _code$driverTypeCode === void 0 ? [] : _code$driverTypeCode, _code$purposeType = code.purposeType, purposeType = _code$purposeType === void 0 ? [] : _code$purposeType;
              _context.next = 12;
              return put({
                type: 'updateState',
                payload: {
                  dataSourceClassList: dataSourceClassList,
                  dataSourceTypeList: dataSourceTypeList,
                  dbPoolTypeList: dbPoolTypeList,
                  dsPurposeCodeList: dsPurposeCodeList,
                  purposeType: purposeType,
                  driverTypeCodeList: driverTypeCodeList
                }
              });

            case 12:
              return _context.abrupt("return", code);

            case 13:
            case "end":
              return _context.stop();
          }
        }
      }, batchCode);
    }),
    // 获取连接池参数
    getDbPoolParams: /*#__PURE__*/_regeneratorRuntime.mark(function getDbPoolParams(_ref3, _ref4) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function getDbPoolParams$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(_getDbPoolParams, payload);

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  dbPoolParams: list
                }
              });

            case 9:
              return _context2.abrupt("return", list);

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, getDbPoolParams);
    }),
    // 获取驱动类
    getDriverClass: /*#__PURE__*/_regeneratorRuntime.mark(function getDriverClass(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function getDriverClass$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_getDriverClass, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, getDriverClass);
    }),
    // 获取数据源列表数据
    fetchDataSourceList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDataSourceList(_ref7, _ref8) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchDataSourceList$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context4.next = 4;
              return call(_fetchDataSourceList, payload);

            case 4:
              res = _context4.sent;
              list = getResponse(res);

              if (!list) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  dataSourceData: list,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchDataSourceList);
    }),
    // 获取数据源详情
    fetchDataSourceDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDataSourceDetail(_ref9, _ref10) {
      var payload, call, put, res, data, _data$options, options;

      return _regeneratorRuntime.wrap(function fetchDataSourceDetail$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call, put = _ref10.put;
              _context5.next = 4;
              return call(_fetchDataSourceDetail, payload);

            case 4:
              res = _context5.sent;
              data = getResponse(res);

              if (!data) {
                _context5.next = 10;
                break;
              }

              _data$options = data.options, options = _data$options === void 0 ? '{}' : _data$options;
              _context5.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  dataSourceDetail: data,
                  dbPoolParams: JSON.parse(options)
                }
              });

            case 10:
              return _context5.abrupt("return", data);

            case 11:
            case "end":
              return _context5.stop();
          }
        }
      }, fetchDataSourceDetail);
    }),
    // 新建保存
    createDataSource: /*#__PURE__*/_regeneratorRuntime.mark(function createDataSource(_ref11, _ref12) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function createDataSource$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call, put = _ref12.put;
              _context6.t0 = getResponse;
              _context6.next = 5;
              return call(_createDataSource, _objectSpread({}, payload));

            case 5:
              _context6.t1 = _context6.sent;
              res = (0, _context6.t0)(_context6.t1);

              if (!res) {
                _context6.next = 10;
                break;
              }

              _context6.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  datasourceId: res.datasourceId
                }
              });

            case 10:
              return _context6.abrupt("return", res);

            case 11:
            case "end":
              return _context6.stop();
          }
        }
      }, createDataSource);
    }),
    // 编辑保存
    editDataSource: /*#__PURE__*/_regeneratorRuntime.mark(function editDataSource(_ref13, _ref14) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function editDataSource$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call;
              _context7.next = 4;
              return call(_editDataSource, _objectSpread({}, payload));

            case 4:
              result = _context7.sent;
              return _context7.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, editDataSource);
    }),
    // 查询服务
    fetchServiceList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchServiceList(_ref15, _ref16) {
      var payload, call, put, _payload$page, page, _payload$size, size, params, res, list;

      return _regeneratorRuntime.wrap(function fetchServiceList$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call, put = _ref16.put;
              _payload$page = payload.page, page = _payload$page === void 0 ? 0 : _payload$page, _payload$size = payload.size, size = _payload$size === void 0 ? 10 : _payload$size, params = _objectWithoutProperties(payload, _excluded);
              _context8.next = 5;
              return call(_fetchServiceList, _objectSpread({
                page: page,
                size: size
              }, params));

            case 5:
              res = _context8.sent;
              list = getResponse(res);

              if (!list) {
                _context8.next = 10;
                break;
              }

              _context8.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  tenantData: list
                }
              });

            case 10:
            case "end":
              return _context8.stop();
          }
        }
      }, fetchServiceList);
    }),
    // 添加服务
    addService: /*#__PURE__*/_regeneratorRuntime.mark(function addService(_ref17, _ref18) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function addService$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              _context9.next = 4;
              return call(_addService, payload);

            case 4:
              result = _context9.sent;
              return _context9.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, addService);
    }),
    // 删除服务
    deleteService: /*#__PURE__*/_regeneratorRuntime.mark(function deleteService(_ref19, _ref20) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteService$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref19.payload;
              call = _ref20.call;
              _context10.next = 4;
              return call(_deleteService, _objectSpread({}, payload));

            case 4:
              result = _context10.sent;
              return _context10.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, deleteService);
    }),
    // 测试数据源
    handleTestDataSource: /*#__PURE__*/_regeneratorRuntime.mark(function handleTestDataSource(_ref21, _ref22) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function handleTestDataSource$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref21.payload;
              call = _ref22.call;
              _context11.next = 4;
              return call(_handleTestDataSource, _objectSpread({}, payload));

            case 4:
              result = _context11.sent;
              return _context11.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, handleTestDataSource);
    }),
    fetchFormParams: /*#__PURE__*/_regeneratorRuntime.mark(function fetchFormParams(_ref23, _ref24) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchFormParams$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref23.payload;
              call = _ref24.call, put = _ref24.put;
              _context12.t0 = getResponse;
              _context12.next = 5;
              return call(_fetchFormParams, _objectSpread({}, payload));

            case 5:
              _context12.t1 = _context12.sent;
              result = (0, _context12.t0)(_context12.t1);

              if (!result) {
                _context12.next = 10;
                break;
              }

              _context12.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  extConfigs: result
                }
              });

            case 10:
              return _context12.abrupt("return", result);

            case 11:
            case "end":
              return _context12.stop();
          }
        }
      }, fetchFormParams);
    }),
    testConnection: /*#__PURE__*/_regeneratorRuntime.mark(function testConnection(_ref25, _ref26) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function testConnection$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              payload = _ref25.payload;
              call = _ref26.call;
              _context13.next = 4;
              return call(_testConnection, payload);

            case 4:
              result = _context13.sent;
              return _context13.abrupt("return", result);

            case 6:
            case "end":
              return _context13.stop();
          }
        }
      }, testConnection);
    }),
    // 请求公钥
    getPublicKey: /*#__PURE__*/_regeneratorRuntime.mark(function getPublicKey(_, _ref27) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function getPublicKey$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              call = _ref27.call, put = _ref27.put;
              _context14.next = 3;
              return call(_getPublicKey);

            case 3:
              res = _context14.sent;

              if (!res) {
                _context14.next = 7;
                break;
              }

              _context14.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  publicKey: res.publicKey
                }
              });

            case 7:
              return _context14.abrupt("return", res);

            case 8:
            case "end":
              return _context14.stop();
          }
        }
      }, getPublicKey);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};